﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ionic.Zip;

namespace TestZip
{
    public partial class UnZip : Form
    {
        public UnZip()
        {
            InitializeComponent();
        }

        private string sFile = "";

        private void BT_Add_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "zip file|*.zip";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();
                sFile = openFile.FileName;
                using (ZipFile zip = ZipFile.Read(openFile.FileName))
                {
                    foreach (ZipEntry zipEntry in zip)
                    {
                        //e.Extract(TargetDirectory, true);  // overwrite == true  
                        listBox1.Items.Add(zipEntry.FileName);
                    }
                }
            }
        }

        private void BT_Save_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            try
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    using (ZipFile zip = ZipFile.Read(sFile))
                    {
                        foreach (ZipEntry zipEntry in zip)
                        {
                            zipEntry.Extract(dialog.SelectedPath, ExtractExistingFileAction.OverwriteSilently);  // overwrite == true  
                        }
                    }
                    MessageBox.Show("unzip ok!", "zip file", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("save error: " + ex.Message, "zip file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                throw;
            }

        }

        private void BT_Exit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
